<?php
class MapperEntry
{
    public function createEntry($name,$text)
    {
        $stmt = Database::getConnection()->prepare("INSERT INTO guestbookshared(`date`, `name`, 
            `entry`) VALUES (now(), :name, :entry) ");
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':entry', $text);
        $row = $stmt->execute();
    //    $entryObject = $this->getModelForRow($row);
        return $row;
    }

    public function getEntries()
    {
        $stmt = Database::getConnection()->prepare("SELECT * FROM `guestbookshared` 
            ORDER BY `date` DESC");
        $stmt->execute();
        $rows = $stmt->fetchAll();
        $entryObjects = $this->getModelsForRows($rows);
        return $entryObjects;
    }

    public function getEntriesByName($name)
    {
        $stmt = Database::getConnection()->prepare("SELECT * FROM `guestbookshared` 
        WHERE `name` = :name ORDER BY `date` DESC");
        $stmt->bindParam(':name', $name);
        $stmt->execute();
        $rows = $stmt->fetchAll();
        $entryObjects = $this->getModelsForRows($rows);
        return $entryObjects;
    }

    public function deleteEntrieById($id)
    {
        $stmt = Database::getConnection()->prepare("DELETE FROM `guestbookshared` 
        WHERE `id` = :id");
        $stmt->bindParam(':id', $id);
        return $stmt->execute();
    }
    
    private function getModelForRow($row)
    {
        $entryObject = null;
        if ($row) {
            $entryObject = new Entry($row->id);
            $entryObject->setName($row->name);
            $entryObject->setText($row->entry);
            $entryObject->setDate($row->date);
        }
        return $entryObject;
    }
    
    private function getModelsForRows($rows)
    {
        $entryObjects = array();
        foreach ($rows as $row) {
            $entryObject = $this->getModelForRow($row);
            if ($entryObject) {
                $entryObjects[$entryObject->getId()] = $entryObject;
            }
        }
        return $entryObjects;
    }
}
?>
